﻿using AutoMapper;
using BW.MesProject.BasicData.Domain.基础;
using BW.MesProject.BasicData.Infrastructure;
using BW.MesProject.Public.ErrorCode;
using BW.MesProject.Public.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace BW.MesProject.BasicData.Application
{
    /// <summary>
    /// 物料组服务实现类
    /// 提供物料组的增删改查等业务功能
    /// </summary>
    public class MaterialgroupServices : IMaterialgroupServices
    {
        private readonly IBaseRepository<Materialgroupmodel> mate;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造函数，通过依赖注入获取仓储和映射器
        /// </summary>
        /// <param name="mate">物料组仓储接口</param>
        /// <param name="mapper">AutoMapper映射器</param>
        public MaterialgroupServices(IBaseRepository<Materialgroupmodel> mate, IMapper mapper)
        {
            this.mate = mate;
            this.mapper = mapper;
        }

        /// <summary>
        /// 添加物料组
        /// </summary>
        /// <param name="model">物料组模型</param>
        /// <returns>添加结果</returns>
        public ApiResult<int> AddMaterialgroup(Materialgroupmodel model)
        {
            var models = mapper.Map<Materialgroupmodel>(model);
            models.Id = YitIdHelper.NextId();
            model.IsDeleted = false;
            if (mate.Add(model) > 0)
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Success,
                    Message = "添加成功",
                    Data = 1
                };
            }
            else
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Message = "添加失败",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 删除物料组（逻辑删除）
        /// </summary>
        /// <param name="id">物料组ID</param>
        /// <returns>删除结果</returns>
        public ApiResult<int> DeleteMaterialgroup(long id)
        {
            //根据id修改逻辑删除为 true
            var model = mate.GetValue(x => x.Id == id);
            model.IsDeleted = true;
            return new ApiResult<int>()
            {
                Code = ApiEnums.Success,
                Message = "删除成功",
                Data = mate.Update(model),
            };
        }

        /// <summary>
        /// 获取所有物料组
        /// </summary>
        /// <returns>所有物料组列表</returns>
        public ApiResult<List<Materialgroupmodel>> GetAllMaterialgroup()
        {
            return new ApiResult<List<Materialgroupmodel>>()
            {
                Code = ApiEnums.Success,
                Message = "查询成功",
                Data = mate.GetAll().ToList(),
            };
        }

        /// <summary>
        /// 分页查询物料组
        /// </summary>
        /// <param name="Materialgroupcode">物料组编码（可选）</param>
        /// <param name="Materialgroupname">物料组名称（可选）</param>
        /// <param name="Materialgrouptype">物料组类型（可选）</param>
        /// <param name="pageindex">页码</param>
        /// <param name="pagesize">每页大小</param>
        /// <returns>分页结果</returns>
        public ApiPaging<List<Materialgroupmodel>> GetMaterialgroup(string? Materialgroupcode, string? Materialgroupname,string? Materialgrouptype, int pageindex, int pagesize)
        {
            // 查询数据
            var list = mate.GetAll().Where(x => x.IsDeleted == false);
            if (Materialgroupcode != null)
            {
                list = list.Where(x => x.Materialgroupcode.Contains(Materialgroupcode));
            }
            if (Materialgroupname != null)
            {
                list = list.Where(x => x.Materialgroupname.Contains(Materialgroupname));
            }
            if (Materialgrouptype != null)
            {
                list = list.Where(x => x.Materialgrouptype.Contains(Materialgrouptype));
            }
            //分页
            list = list.Skip((pageindex - 1) * pagesize).Take(pagesize);
            return new ApiPaging<List<Materialgroupmodel>>()
            {
                Code = ApiEnums.Success,
                Message = "查询成功",
                Data = list.ToList(),
                TotalCount = list.Count(),
            };
        }

        /// <summary>
        /// 根据ID获取物料组
        /// </summary>
        /// <param name="id">物料组ID</param>
        /// <returns>物料组信息</returns>
        public ApiResult<Materialgroupmodel> GetMaterialgroupById(long id)
        {
            //根据id查询
            var model = mate.GetValue(x => x.Id == id);
            return new ApiResult<Materialgroupmodel>()
            {
                Code = ApiEnums.Success,
                Message = "查询成功",
                Data = model
            };
        }

        /// <summary>
        /// 更新物料组信息
        /// </summary>
        /// <param name="model">物料组模型</param>
        /// <returns>更新结果</returns>
        public ApiResult<int> UpdateMaterialgroup(Materialgroupmodel model)
        {
            // 更新
            return new ApiResult<int>()
            {
                Code = ApiEnums.Success,
                Message = "修改成功",
                Data = mate.Update(model)
            };
        }
    }
}
